package com.yz.book.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yz.book.service.ProductService;
import com.yz.book.vo.EsProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ste
 * @since 2023-02-04 03:37:37
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;


    @RequestMapping("all")
    @ResponseBody
    public Page all(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "9") Integer pageSize,
            @RequestParam(defaultValue = "") String keyword
    ) {
        System.out.println(keyword);
        Page page = new Page<>(currentPage, pageSize);
        Page productPage = null;
        if ("".equals(keyword)) {
            productPage = productService.page(page);
        } else {
//            productPage = productService.searchBySQL(page, keyword);
            Page<EsProduct> esProductPage = productService.searchByES(currentPage, pageSize, keyword);
            productPage = esProductPage;
        }
        System.out.println("__________________");
        return productPage;
    }


//    @RequestMapping("search")
//    public String search(String keyword, Model model){
//
//        model.addAttribute("keyword",keyword);
//
//        return "forward:/";
//    }
}

